//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.farm.web.online;

import com.farm.core.auth.domain.LoginUser;
import com.farm.core.sql.result.DataResult;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;

public class OnlineUserOpImpl implements OnlineUserOpInter {
    private String ip;
    private HttpSession httpSession;
    private Map<String, Object> strutsSession;

    public OnlineUserOpImpl() {
    }

    public DataResult findOnlineUser() {
        List<Map<String, Object>> list = new ArrayList();
        List<String> removekeys = new ArrayList();
        Iterator var4 = OnlineUserOpInter.onlineUserTable.keySet().iterator();

        String key;
        while(var4.hasNext()) {
            key = (String)var4.next();
            Date date = (Date)((Map)OnlineUserOpInter.onlineUserTable.get(key)).get("TIME");
            Date visitdate = (Date)((Map)OnlineUserOpInter.onlineUserTable.get(key)).get("STARTTIME");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat curentDayf = new SimpleDateFormat("yyyy-MM-dd");
            Date curentTime = new Date();
            Calendar curentC = Calendar.getInstance();
            curentC.setTime(curentTime);
            Calendar curentV = Calendar.getInstance();
            curentV.setTime(date);
            Calendar curentl = Calendar.getInstance();
            curentl.setTime(visitdate);
            long timeMillis = (curentC.getTimeInMillis() - curentV.getTimeInMillis()) / 60000L;
            long visitMillis = (curentC.getTimeInMillis() - curentl.getTimeInMillis()) / 60000L;
            if (timeMillis > 20L) {
                removekeys.add(key);
            } else {
                Map<String, Object> map = new HashMap();
                map.put("LNAME", key);
                map.put("VISITTIME", visitMillis);
                map.put("TIME", sdf.format(date).replace(curentDayf.format(new Date()), "今天"));
                LoginUser user = (LoginUser)((Map)OnlineUserOpInter.onlineUserTable.get(key)).get("USEROBJ");
                map.put("USEROBJ", user);
                if (user != null) {
                    map.put("USERNAME", user.getName());
                    map.put("LOGINNAME", user.getLoginname());
                    map.put("LOGINTIME", sdf.format(((Map)OnlineUserOpInter.onlineUserTable.get(key)).get("LOGINTIME")).replace(curentDayf.format(new Date()), "今天"));
                } else {
                    map.put("USERNAME", "NONE");
                    map.put("LOGINNAME", "NONE");
                }

                map.put("IP", ((Map)OnlineUserOpInter.onlineUserTable.get(key)).get("IP"));
                map.put("STARTTIME", sdf.format(((Map)OnlineUserOpInter.onlineUserTable.get(key)).get("STARTTIME")).replace(curentDayf.format(new Date()), "今天"));
                list.add(map);
            }
        }

        var4 = removekeys.iterator();

        while(var4.hasNext()) {
            key = (String)var4.next();
            OnlineUserOpInter.onlineUserTable.remove(key);
        }

        Collections.sort(list, new Comparator<Map<String, Object>>() {
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                return ((String)o1.get("TIME")).compareTo((String)o2.get("TIME"));
            }
        });
        DataResult result = DataResult.getInstance(list, list.size(), 1, list.size());
        return result;
    }

    public void userVisitHandle() {
        if (this.httpSession == null && this.strutsSession == null && this.ip != null) {
            throw new RuntimeException("参数错误");
        } else {
            Map<String, Object> userMap = null;
            if (OnlineUserOpInter.onlineUserTable.get(this.ip) != null) {
                userMap = (Map)OnlineUserOpInter.onlineUserTable.get(this.ip);
            } else {
                userMap = new HashMap();
                ((Map)userMap).put("IP", this.ip);
                ((Map)userMap).put("STARTTIME", new Date());
            }

            ((Map)userMap).put("TIME", new Date());
            Object user = this.httpSession != null ? this.httpSession.getAttribute("USEROBJ") : this.strutsSession.get("USEROBJ");
            if (user != null) {
                ((Map)userMap).put("LNAME", ((LoginUser)user).getLoginname());
                if (((Map)userMap).get("USEROBJ") == null) {
                    ((Map)userMap).put("LOGINTIME", new Date());
                }

                ((Map)userMap).put("USEROBJ", user);
            } else {
                ((Map)userMap).put("USEROBJ", (Object)null);
            }

            OnlineUserOpInter.onlineUserTable.put(this.ip, userMap);
            if ((long)OnlineUserOpInter.onlineUserTable.size() > 5000L) {
                OnlineUserOpInter.onlineUserTable.clear();
            }

        }
    }

    public String getIp() {
        return this.ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public HttpSession getHttpSession() {
        return this.httpSession;
    }

    public void setHttpSession(HttpSession httpSession) {
        this.httpSession = httpSession;
    }

    public Map<String, Object> getStrutsSession() {
        return this.strutsSession;
    }

    public void setStrutsSession(Map<String, Object> strutsSession) {
        this.strutsSession = strutsSession;
    }

    public static OnlineUserOpInter getInstance(String ip, HttpSession httpSession) {
        OnlineUserOpImpl obj = new OnlineUserOpImpl();
        obj.setHttpSession(httpSession);
        obj.setIp(ip);
        return obj;
    }

    public static OnlineUserOpInter getInstance(String ip, Map<String, Object> strutsSession) {
        OnlineUserOpImpl obj = new OnlineUserOpImpl();
        obj.setIp(ip);
        obj.setStrutsSession(strutsSession);
        return obj;
    }

    public static OnlineUserOpInter getInstance() {
        OnlineUserOpImpl obj = new OnlineUserOpImpl();
        return obj;
    }
}
